/*
 * Copyright (c) 2025 GuoKaiHong (Shenzhen) Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import { initCallback } from "./i_printer_service";
import { selfCheckCallback } from "./i_printer_service";
import { printStringCallback } from "./i_printer_service";
import { printChinaStringCallback } from "./i_printer_service";
import { printCutPaperCallback } from "./i_printer_service";
import { printChargeRowCallback } from "./i_printer_service";
import { setAlignmentCallback } from "./i_printer_service";
import { setSizeTextCallback } from "./i_printer_service";
import { setBoldCallback } from "./i_printer_service";
import { setSizeCharCallback } from "./i_printer_service";
import { setSizeChineseCallback } from "./i_printer_service";
import { printFeedDotCallback } from "./i_printer_service";
import { setCloseCallback } from "./i_printer_service";
import { setCleanCallback } from "./i_printer_service";
import { getStatusCallback } from "./i_printer_service";
import { printQrcodeCallback } from "./i_printer_service";
import { print1DBarCallback } from "./i_printer_service";
import { printDiskImgFileCallback } from "./i_printer_service";
import { printDiskBmpFileCallback } from "./i_printer_service";
import IPrinterService from "./i_printer_service";
import rpc from "@ohos.rpc";

export default class PrinterServiceProxy implements IPrinterService {
  constructor(proxy) {
    this.proxy = proxy;
  }

  init(deviceId: number, callback: initCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(deviceId);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_INIT, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  selfCheck(callback: selfCheckCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_SELF_CHECK, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        callback(_errCode);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  printString(strData: string, iImme: number, callback: printStringCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeString(strData);
    _data.writeInt(iImme);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT_STRING, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  printChinaString(strData: string, iImme: number, callback: printChinaStringCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeString(strData);
    _data.writeInt(iImme);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT_CHINA_STRING, _data, _reply, _option)
      .then(function (result) {
        if (result.errCode === 0) {
          let _errCode = result.reply.readInt();
          if (_errCode != 0) {
            let _returnValue = undefined;
            callback(_errCode, _returnValue);
            return;
          }
          let _returnValue = result.reply.readInt();
          callback(_errCode, _returnValue);
        } else {
          console.log("sendRequest failed, errCode: " + result.errCode);
        }
      })
  }

  printCutPaper(iMode: number, callback: printCutPaperCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(iMode);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT_CUT_PAPER, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  printChargeRow(callback: printChargeRowCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT_CHARGE_ROW, _data, _reply, _option)
      .then(function (result) {
        if (result.errCode === 0) {
          let _errCode = result.reply.readInt();
          if (_errCode != 0) {
            let _returnValue = undefined;
            callback(_errCode, _returnValue);
            return;
          }
          let _returnValue = result.reply.readInt();
          callback(_errCode, _returnValue);
        } else {
          console.log("sendRequest failed, errCode: " + result.errCode);
        }
      })
  }

  setAlignment(iAlignment: number, callback: setAlignmentCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(iAlignment);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_SET_ALIGNMENT, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  setSizeText(iHeight: number, iWidth: number, callback: setSizeTextCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(iHeight);
    _data.writeInt(iWidth);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_SET_SIZE_TEXT, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  setBold(iBold: number, callback: setBoldCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(iBold);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_SET_BOLD, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  setSizeChar(iHeight: number, iWidth: number, iUnderline: number, iAsciiType: number,
    callback: setSizeCharCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(iHeight);
    _data.writeInt(iWidth);
    _data.writeInt(iUnderline);
    _data.writeInt(iAsciiType);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_SET_SIZE_CHAR, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  setSizeChinese(iHeight: number, iWidth: number, iUnderline: number, iChineseType: number,
    callback: setSizeChineseCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(iHeight);
    _data.writeInt(iWidth);
    _data.writeInt(iUnderline);
    _data.writeInt(iChineseType);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_SET_SIZE_CHINESE, _data, _reply, _option)
      .then(function (result) {
        if (result.errCode === 0) {
          let _errCode = result.reply.readInt();
          if (_errCode != 0) {
            let _returnValue = undefined;
            callback(_errCode, _returnValue);
            return;
          }
          let _returnValue = result.reply.readInt();
          callback(_errCode, _returnValue);
        } else {
          console.log("sendRequest failed, errCode: " + result.errCode);
        }
      })
  }

  printFeedDot(iNumber: number, callback: printFeedDotCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(iNumber);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT_FEED_DOT, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  setClose(callback: setCloseCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_SET_CLOSE, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  setClean(callback: setCleanCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_SET_CLEAN, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  getStatus(callback: getStatusCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_GET_STATUS, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  printQrcode(strData: string, iLMargin: number, iMSide: number, iRound: number, callback: printQrcodeCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeString(strData);
    _data.writeInt(iLMargin);
    _data.writeInt(iMSide);
    _data.writeInt(iRound);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT_QRCODE, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  print1DBar(iWidth: number, iHeight: number, iHriSize: number, iHriSeat: number, iCodeType: number, strData: string,
    callback: print1DBarCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeInt(iWidth);
    _data.writeInt(iHeight);
    _data.writeInt(iHriSize);
    _data.writeInt(iHriSeat);
    _data.writeInt(iCodeType);
    _data.writeString(strData);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT1_D_BAR, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  printDiskImgFile(strPath: string, callback: printDiskImgFileCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeString(strPath);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT_DISK_IMG_FILE, _data, _reply, _option)
      .then(function (result) {
        if (result.errCode === 0) {
          let _errCode = result.reply.readInt();
          if (_errCode != 0) {
            let _returnValue = undefined;
            callback(_errCode, _returnValue);
            return;
          }
          let _returnValue = result.reply.readInt();
          callback(_errCode, _returnValue);
        } else {
          console.log("sendRequest failed, errCode: " + result.errCode);
        }
      })
  }

  printDiskBmpFile(strPath: string, callback: printDiskBmpFileCallback): void {
    let _option = new rpc.MessageOption();
    let _data = rpc.MessageSequence.create();
    let _reply = rpc.MessageSequence.create();
    _data.writeString(strPath);
    this.proxy.sendRequest(PrinterServiceProxy.COMMAND_PRINT_DISK_BMP_FILE, _data, _reply, _option)
      .then(function (result) {
        if (result.errCode === 0) {
          let _errCode = result.reply.readInt();
          if (_errCode != 0) {
            let _returnValue = undefined;
            callback(_errCode, _returnValue);
            return;
          }
          let _returnValue = result.reply.readInt();
          callback(_errCode, _returnValue);
        } else {
          console.log("sendRequest failed, errCode: " + result.errCode);
        }
      })
  }

  static readonly COMMAND_INIT = 1;
  static readonly COMMAND_SELF_CHECK = 2;
  static readonly COMMAND_PRINT_STRING = 3;
  static readonly COMMAND_PRINT_CHINA_STRING = 4;
  static readonly COMMAND_PRINT_CUT_PAPER = 5;
  static readonly COMMAND_PRINT_CHARGE_ROW = 6;
  static readonly COMMAND_SET_ALIGNMENT = 7;
  static readonly COMMAND_SET_SIZE_TEXT = 8;
  static readonly COMMAND_SET_BOLD = 9;
  static readonly COMMAND_SET_SIZE_CHAR = 10;
  static readonly COMMAND_SET_SIZE_CHINESE = 11;
  static readonly COMMAND_PRINT_FEED_DOT = 12;
  static readonly COMMAND_SET_CLOSE = 13;
  static readonly COMMAND_SET_CLEAN = 14;
  static readonly COMMAND_GET_STATUS = 15;
  static readonly COMMAND_PRINT_QRCODE = 16;
  static readonly COMMAND_PRINT1_D_BAR = 17;
  static readonly COMMAND_PRINT_DISK_IMG_FILE = 18;
  static readonly COMMAND_PRINT_DISK_BMP_FILE = 19;
  private proxy
}

